import { ref } from 'vue';

export const useUpdateRemark = () => {
  const curUpdateState = ref<null | { remark: string; type: number }>(null);
  const getUpdateRemarkByType = (remark1?: string, remark2?: string, type?: number) => {
    const _remark1 = remark1 ?? '';
    const _remark2 = remark2 ? ' ' + remark2 : '';
    // debugger;
    if (!type == undefined) return _remark1;
    let _updateType: 0 | 1 = 0; // 0追加； 1 替换
    const { remark: _r1, type: _t1 } = curUpdateState.value || {};
    if (_t1 === type) _updateType = 1;
    let res = _remark1;
    if (_updateType === 0) {
      res += _remark2;
      curUpdateState.value = { remark: _remark2, type: type as number };
    } else if (_updateType === 1) {
      res = res?.replace(new RegExp(`${_r1}$`), _remark2);
      curUpdateState.value = { remark: _remark2, type: type as number };
    }
    return res;
  };

  const resetRemarkState = () => {
    curUpdateState.value = null;
  };

  return {
    getUpdateRemarkByType,
    resetRemarkState
  };
};
